/*
 * Flowtext lib v0.1
 *
 * @name flowtext.js
 * @author Mark van 't Zet - http://mvantzet.wordpress.com
 * @version 0.1
 * @date January 8, 2009
 * @category jQuery library
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

$(function() {
	$('img.flowtext').each(function() {
		var oImage = this;
		var sAlign = $(oImage).css('float');
		if (sAlign != 'left' && sAlign != 'right') {
			sAlign = $(oImage).attr('align');
			if (sAlign != 'left' && sAlign != 'right') {
				return;
			}
		}

		var sUrl = this.src + '.flowtext.xml' + (window.flowtextDebug ? '?' + new Date() : '');

		$.get(sUrl, function (xml, textStatus) {
			if (typeof xml == 'string') {
				if (window.flowtextDebug) alert('XML is returned as string, is the content type for XML files correct on the web server?');
				return;
			}
			var nLeft = $(oImage).offset().left;	
			var nTop = $(oImage).offset().top;	
			$('outlines outline', xml).each(function() {
				$(oImage).before('<div class="flowtext-outline" style="float:' + sAlign + ';clear:' + sAlign + ';width:' + $(this).attr('width') + ';height:' + $(this).attr('height') + ';"></div>');
			});
			$(oImage).css('position', 'absolute');
			$(oImage).css('left', nLeft + 'px');
			$(oImage).css('top', nTop + 'px');
			$(oImage).css('z-index', '-1');
		}, 'xml');
	});
});